<?php 

Class menuController extends appController
{
	/**
	 * 显示菜单栏目列表
	 */
	public function index()
	{
	   	$page = yf_empty($_REQUEST['page'], 1, true);
	   	$sort['keyword'] = yf_empty(trim($_GET['k']), "", true);
	   	$sort['type'] = yf_empty(trim($_GET['c']), "", true);
	   	$menu_list = Model('menu')->get_cat_list($sort);
		$this->assign('search_action', '1');
		$this->assign('menu_list', $menu_list);
		$this->assign('menu_page', $menu_list['pagelink']);
		$this->display('menu_list.html');
	}
	
	/**
	 * 
	 */
	public function ordering()
	{
		$id = yf_empty(intval($_POST['id']), '', true);
		$val = yf_empty(intval($_POST['val']), '', true);
		$result = Model('menu')->ordering($val, $id);
		$this->showMsg($result);
	}
	
	/**
	 * 显示添加菜单页面
	 */
	public function add_menu()
	{
		$this->admin_priv('addmenu');
		$this->assign('parent_list', Model('menu')->get_cat_list());
		$this->assign('action','create_menu');
		$this->display('category.html');
	}
	
	/**
	 * 添加栏目信息
	 */
	public function create_menu()
	{
		$this->admin_priv('addmenu');
		$info['parent_id'] = yf_empty($_POST['parent_id'], 0, true);
		$info['name'] = yf_empty($_POST['name'], Lang('title_empty'));
		$en_name = yf_empty($_POST['en_name'], "", true);
		$info['name'] = CH_SC.$info['name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['is_show'] = yf_empty($_POST['is_show'], 1, true);
		$info['nav_pos'] = is_array($_POST['nav_pos']) ? implode(',', $_POST['nav_pos']) : $_POST['nav_pos'];
		$info['code'] = yf_empty(trim(strtolower($_POST['code'])), "栏目别名不能为空");
		$info['is_extend_links'] = yf_empty(intval($_POST['is_extend_links']), 0, true);
		$info['thumb'] = yf_empty($_POST['thumb'], "", true);
		
		$data = array('error' => 1, 'content' => '');
		
		$info['code'] = str_replace(' ', '', $info['code']);
		
		if (hy_check($info['code'],'un')=='#@_error')
		{
			$data['content']=Lang('code_error');
			$this->showMsg($data);
		}
	
		if($info['is_extend_links'] == 1)
		{
			$info['redirect_url'] = yf_empty($_POST['redirect_url'], "", true);
			if (hy_check($info['redirect_url'], 'u') == '#@_error' && $info['redirect_url'] != 'http://')
			{
		 		$data['content']=Lang('url_error');
		 		$this->showMsg($data);
		 	}
		}
		else 
		{
			$info['type'] = yf_empty($_POST['type'], "", true);
			$info['keywords'] = yf_empty($_POST['keywords'], "", true);
			$info['page_title'] = yf_empty($_POST['page_title'], "", true);
			$info['description'] = yf_empty($_POST['description'], "", true);
		}
		
		if(Model('menu')->check_code($info['code']))
		{
		   	$result = Model('menu')->insert_menu($info);
		  	if($result)
		   	{
		   		$data['error'] = 0;
				$data['content'] = Lang('SUCCESS_ADD');
		   	}
		   	else
		  	{
				$data['content'] = Lang('INVALID_OPERATION');
		   	}
		   	$this->showMsg($data);
		}
		else 
		{
			$data['content'] = Lang('CODE_REPEATING');
			$this->showMsg($data);
		}
	} 
	
	/**
	 * 删除菜单栏目
	 */
	public function del()
	{
		$this->admin_priv('delmenu', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$result = Model('menu')->del_menu($id);
		$this->showMsg($result);
	}
	
	/**
	 * 菜单栏目编辑
	 */
	public function edit_menu()
	{
		$this->admin_priv('editmenu', 2);
		$id = yf_empty(intval($_GET['id']), "", true);
		$menu_info = Model('menu')->get_menu_info($id);
		$this->assign('parent_list', Model('menu')->get_cat_list());
		$this->assign('menu',$menu_info);
		$this->assign('id',$id);
		$this->assign('ur_here','>编辑栏目');
		$this->assign('action','update_menu');
		$this->display('category.html');
	
	}
	
	/**
	 * 更新菜单栏目信息
	 */
	public function update_menu()
	{
		$this->admin_priv('editmenu');
		$id = yf_empty(intval($_POST['id']), '', true);
		$info['parent_id'] = yf_empty(intval($_POST['parent_id']), 0, true);
		$info['name'] = yf_empty($_POST['name'], Lang('title_empty'));
		$en_name = yf_empty($_POST['en_name'], "", true);
		$info['name'] = CH_SC.$info['name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['is_show'] = yf_empty(intval($_POST['is_show']), 0, true);
		$info['nav_pos'] = is_array($_POST['nav_pos']) ? implode(',', $_POST['nav_pos']) : $_POST['nav_pos'];
		$info['code'] = yf_empty(trim(strtolower($_POST['code'])), "栏目别名不能为空");	
		$info['is_extend_links'] = yf_empty(intval($_POST['is_extend_links']), 0, true);
		
		$data = array('error' => 1, 'content' => '');
		
		if($info['parent_id'] == $id)
		{
			$data['content'] = Lang('PARENT_ID_ERR');
			$this->showMsg($data);
		}
		
		if(hy_check($info['code'],'un') == '#@_error')
		{
			$data['content']=Lang('code_error');
			$this->showMsg($data);
		}
		
		if($info['is_extend_links'] == 1)
		{
			$info['redirect_url'] = yf_empty($_POST['redirect_url'], "", true);
			if(hy_check($info['redirect_url'],'u') == '#@_error' && $info['redirect_url'] != 'http://')
			{
				$data['content']=Lang('url_error');
				$this->showMsg($data);
			}
			$info['type'] = '';
			$info['page_title'] = '';
			$info['keywords'] = '';
			$info['description'] = '';
		}
		else
		{
			
			$info['thumb'] = yf_empty($_POST['thumb'], '', true);
			$info['type'] = yf_empty($_POST['type'], '', true);
			$info['page_title'] = yf_empty($_POST['page_title'], '', true);
			$info['keywords'] = yf_empty($_POST['keywords'], '', true);
			$info['description'] = yf_empty($_POST['description'], '', true);
			$info['redirect_url'] = '';
		}
		
		if(Model('menu')->check_code($info['code'], $id))
		{
			$result = Model('menu')->update_menu($info, $id);
			if($result)
			{
				$data['error'] = 0;
				$data['content']=Lang('SUCCESS_EDIT');
			}
			else
			{
				$data['content']=Lang('INVALID_OPERATION');
			}
			$this->showMsg($data);
		}
		else
		{
			$data['content']=Lang('CODE_REPEATING');
			$this->showMsg($data);
		}
	}
	
}